<?php

// $Id$


require_once($WSC->get_site_inc_dir().'modules/tree_menu/tree_menu_dbc.class.php');
require_once('tree_menu_item_ml_dbc.class.php');


class Tree_Menu_ML_DBC extends Tree_Menu_DBC {
	private $lang;


	function __construct($table = 'menu', $lang) {
		$this->set_table($table);
		$this->set_lang($lang);
	}


	public function set_lang($lang) {
		$this->lang = $lang;
	}


	public function get_lang() {
		return $this->lang;
	}


	public function load_menu(DB_Connection $dbc, $full = false) {
		$ret = false;

		$table = addslashes($this->get_table());

		$lang = $this->get_lang();

		$sql = "select id from $table where parent = 0 order by `order` asc";
		$rez = $dbc->execute($sql);
		while ($rez_obj = $rez->fetch_object()) {
			$item = new Tree_Menu_Item_ML_DBC($this->get_table(), $lang);
			if ($full) {
				$ret = $item->load_by_id_full($dbc, $rez_obj->ID);
			} else {
				$ret = $item->load_by_id($dbc, $rez_obj->ID);
			}
			$this->add_item(clone $item);
		}

		return $ret;
	}


	public function save(DB_Connection $dbc) {
		foreach($this->items as $item) {
			$item->save($dbc);
		}
	}


	public function add_item(Tree_Menu_Item $item) {
		if ($item instanceof Tree_Menu_Item_ML_DBC) {
			parent::add_item($item);
		} else {
			throw new TE_Tree_Menu('$item is not instance of Tree_Menu_Item_ML_DBC');
		}
	}


	public function import_from_file($file) {
		$ret = false;

		$xml = @simplexml_load_file($file);

		if ($xml) {
			$new_items = array();
			foreach($xml->menuitem as $item) {
				$raw_arr[0] = trim($item->label);
				$raw_arr[1] = trim(strtolower($item->parenthid));
				$raw_arr[2] = trim($item->link);
				$raw_arr[3] = trim($item->hid);
				$raw_arr[4] = trim($item->target);
				$raw_arr[5] = trim($item->order);

				if ($item->mllabels) {
					$raw_arr['ml_labels'] = array();
//					foreach($item->mllabels) {
//						$raw_arr['ml_labels'][][]
//					}
				}

				$new_items[] = Tree_Menu_Item_ML_DBC::create_from_import_data($raw_arr, $this->get_table());
				$ret = $new_items;
			}
		} else {
			throw new TE_Tree_Menu_DBC_Import_File_Failed('File '.$file.' is not valid import XML file.');
		}

		return $ret;
	}


	public function update_item(Tree_Menu_Item_DBC $item) {
		if ($item instanceof Tree_Menu_Item_ML_DBC) {
			if ($this->item_exists($item)) {

				$orig_item = $this->items[$item->get_hid()];
				$orig_mls = $orig_item->get_ml_labels();

				$new_mls = $item->get_ml_labels();

				foreach($orig_mls as $orig_ml) {
					foreach($new_mls as $new_ml) {
						if ($new_ml->get_language() == $orig_ml->get_language()) {
							$new_ml->set_id($orig_ml->get_id());
							$item->update_ml_label($new_ml);
							break;
						}
					}
				}

				$item->set_id($orig_item->get_id());
				$item->set_subitems($orig_item->get_subitems());
				$this->items[$item->get_hid()] = $item;


	//			$id = $this->items[$item->get_hid()];
	//			$item->set_id($item);
	//			$this->items[$item->get_hid()] = $item;
			} else {
				throw new TE_Tree_Menu('Such item does not exists: '.$item->get_label());
			}
		} else {
			throw new TE_Tree_Menu('$item is not instance of Tree_Menu_Item_ML_DBC.');
		}
	}
}


